package cn.yunhe.services;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;


import cn.yunhe.dao.RentDao;
import cn.yunhe.model.House;
import cn.yunhe.model.Keywords;
import cn.yunhe.model.Street;
import cn.yunhe.model.Type;
import cn.yunhe.model.UserRegs;
import cn.yunhe.until.SqlSessioinFactoryUtil;

public class SerRent {

	/*
	 * 注册用户
	 * **/
   SqlSession session = SqlSessioinFactoryUtil.getSqlSession();
	
   RentDao rentDao = session.getMapper(RentDao.class);
	public int addUser(UserRegs userRegs) {
		
	int flag=rentDao.addUser(userRegs);
		session.commit();
		return flag;
	}
	
	/*
	 * 用户登录
	 * **/
	public UserRegs login(String name, String password) {
		Map<String, String> map =new HashMap<String, String>();
		  
		   map.put("uname", name);
		   map.put("password", password);
		   UserRegs userRegs =rentDao.login(map);
           return userRegs;
	
	}
	
	/*
	 * 添加房屋
	 * **/
	
	public int addHouse(House house) {
		
		
		int falg =rentDao.addHouse(house);  
		
		session.commit();
		return falg;
		
	}
	
	
	
	
	
	
	/*
	 * 查詢房屋街道Id
	 * **/
	  public Street selIdByStreet(String name) {
		
		  Street street= rentDao.selIdByStreet(name);
		  return street;
	}
	
	/*
	 * 查詢房屋類型
	 * */
	
	public Type selIdByType(String name) {
		  
		Type  type =rentDao.selIdByType(name);
	    return type;
	}
	/*
	 * 查询所有某人的所有
	 * **/
	
	
	public House selAll(House house) {
		
		House hou= rentDao.selAll(house);
		
		return hou;
	}
	
	public int getCount() {
	
		return	rentDao.getCount();
	}
	public List<House> selectAll(int pageIndex, int pageSize){
		
		pageIndex = (pageIndex-1)*pageSize;
		
		return rentDao.selectAll(pageIndex, pageSize);
		
	}
	
	/*
	 * 删除房屋
	 * 
	 * **/
	public int delXinXi(House house) {
		
	 int falg =rentDao.delXinXi(house);
	
	  session.commit();
	  return falg;
	
	}
	/**
	 * 修改房屋信息
	 * */
	 public int updateXinxi(House house){
		 int falg = rentDao.updateXinxi(house);
		  session.commit();
		  return falg;
	 }
	

		//多条件获取住房总量
		public int getHouseCountByKeywords(String title,String street,String type,String floorage,String price,int currpage) {
			float beginFloorage = 0;
			float endFloorage = 0;
			float beginPrice = 0;
			float endPrice = 0;
			int streetid = 0;
			int typeid = 0;
			
			
			if(!"".equals(floorage)&&floorage!=null) {
				String floor[] = floorage.split("-");
				beginFloorage = Integer.parseInt(floor[0]);
				endFloorage = Integer.parseInt(floor[1]);
			}
			if(!"".equals(price)&&price!=null){
				String pri[] = price.split("-");
				beginPrice = Integer.parseInt(pri[0]);
				endPrice = Integer.parseInt(pri[1]);
			}
			if(!"".equals(type)&&type!=null){
				typeid=rentDao.selIdByType(type).getTid();
			}
			if(!"".equals(street)&&street!=null){
				streetid=rentDao.selIdByStreet(street).getSid();
			}
			Keywords keywords=new Keywords(title, streetid,typeid, beginFloorage, endFloorage,
					beginPrice ,endPrice, currpage);
			return rentDao.getHouseCountByKeywords(keywords);
		}
		
		//多条件获取住房
		public List<House> getHouseByKeyword(String title,String street,String type,String floorage,String price,int currpage){
			float beginFloorage = 0;
			float endFloorage = 0;
			float beginPrice = 0;
			float endPrice = 0;
			int streetid = 0;
			int typeid = 0;
			
			
			currpage=(currpage-1)*3;
			if(!"".equals(floorage)&&floorage!=null) {
				String floor[] = floorage.split("-");
				beginFloorage = Integer.parseInt(floor[0]);
				endFloorage = Integer.parseInt(floor[1]);
			}
			if(!"".equals(price)&&price!=null){
				String pri[] = price.split("-");
				beginPrice = Integer.parseInt(pri[0]);
				endPrice = Integer.parseInt(pri[1]);
			}
			if(!"".equals(type)&&type!=null){
				typeid=rentDao.selIdByType(type).getTid();
			}
			if(!"".equals(street)&&street!=null){
				streetid=rentDao.selIdByStreet(street).getSid();
			}
			Keywords keywords=new Keywords(title, streetid,typeid, beginFloorage, endFloorage,
					beginPrice ,endPrice, currpage);
			return rentDao.getHouseByKeyword(keywords);
		}
	
	
	
	
	
}
